﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Mukhopatro.BusinessEntities;
using Mukhopatro.BusinessLogicLayer;
using Mukhopatro.Helpers;

/// <summary>
/// Summary description for SessionManager
/// </summary>
public class SessionManager
{
    

    public static Boolean _IS_SUPER_ADMIN
    {
        get
        {
            if (HttpContext.Current.Session["_IS_SUPER_ADMIN"] != null)
            {
                return Convert.ToBoolean(HttpContext.Current.Session["_IS_SUPER_ADMIN"]);
            }
            else
            {
                return false;
            }
        }
        set
        {
            HttpContext.Current.Session["_IS_SUPER_ADMIN"] = value;
        }
    }

    public static int CustomerId
    {
        get
        {
            if (HttpContext.Current.Session["_MUKHOPATRO_CustomerId"] == null)
            {
                LoadCustomerId();                
            }
            
            return Convert.ToInt32(HttpContext.Current.Session["_MUKHOPATRO_CustomerId"]);
        }
        set
        {
            HttpContext.Current.Session["_MUKHOPATRO_CustomerId"] = value;
        }
    }


    public static Customer _LOGGED_IN_CUSTOMER
    {
        get
        {
            if (HttpContext.Current.Session["_LOGGED_IN_CUSTOMER"] != null)
            {
                return HttpContext.Current.Session["_LOGGED_IN_CUSTOMER"] as Customer;
            }
            else
            {
                return null;
            }
        }
        set
        {
            HttpContext.Current.Session["_LOGGED_IN_CUSTOMER"] = value;
        }
    }

    public static CustomerSetting SITE_CUSTOMER_SETTINGS
    {
        get
        {
            if (HttpContext.Current.Session["_MUKHOPATRO_SITE_CUSTOMER_SETTINGS"] == null)
            {
                LoadCustomerSettings();
            }
            
            return HttpContext.Current.Session["_MUKHOPATRO_SITE_CUSTOMER_SETTINGS"] as CustomerSetting;
        }
        set
        {
            HttpContext.Current.Session["_MUKHOPATRO_SITE_CUSTOMER_SETTINGS"] = value;
        }
    }


    private static void LoadCustomerSettings()
    {
        CustomerSettingBLL bll = new CustomerSettingBLL();
        CustomerSetting setting = bll.GetCustomerSettings(SessionManager.CustomerId);
        HttpContext.Current.Session["_MUKHOPATRO_SITE_CUSTOMER_SETTINGS"] = setting;
    }

    private static void LoadCustomerId()
    {
        CustomerBLL bll = new CustomerBLL();
        Customer customer = bll.GetCustomer(obj => obj.ClientId == SiteSettings._CLIENT_ID);
        HttpContext.Current.Session["_MUKHOPATRO_CustomerId"] = customer.Id;
    }
}
